<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2020-09-03 10:18:10
 * @LastEditors: Sam
 * @LastEditTime: 2020-09-03 19:37:21
 */
/**
 *
 * @author: xaboy<365615158@qq.com>
 * @day: 2017/11/11
 */

namespace app\admin\controller\user;

use app\admin\controller\AuthController;
use think\facade\Route as Url;
use basic\BaseModel;
use think\facade\Request;
use services\{FormBuilder as Form, UtilService as Util, JsonService as Json};
use app\admin\model\user\User as UserModel;

/**
 * 用户管理控制器
 * Class User
 * @package app\admin\controller\user
 */
class User extends AuthController
{
    

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $this->assign('count_user', UserModel::getcount());
        return $this->fetch();
    }


    /**
     * 获取user表
     *
     * @return json
     */
    public function get_user_list()
    {
        $where = Util::getMore([
            ['page', 1],
            ['limit', 20],
            ['nickname', ''],
            ['status', ''],     
            ['order', ''],
            ['data', ''],  
            ['country', ''],
            ['province', ''],
            ['city', ''],
            ['sex', ''],
  
        ]);
        return Json::successlayui(UserModel::getUserList($where));
    }

    
    /**
     * 快速编辑
     *
     * @return json
     */
    public function set_value($field='',$id='',$value=''){
        $field=='' || $id=='' || $value=='' && Json::fail('缺少参数');
        if(UserModel::where(['id'=>$id])->update([$field=>$value]))
            return Json::successful('保存成功');
        else
            return Json::fail('保存失败');
    }

    
    /*
     * 删除
     * @param int $id
     * */
    public function delete($id=0)
    {
        $id = input('param.id/a');
        !$id && JsonService::fail('缺少参数');
        if(UserModel::where('id','in',$id)->delete())
            return JsonService::successful('删除成功');
        else
            return JsonService::fail('删除失败');
    }

    
    /**
     * 编辑模板消息
     * @param $id
     * @return mixed|\think\response\Json|void
     */
    public function form()
    {
        $uid = Request::param('id/d');
        if($uid){
            $user = UserModel::get($uid);
            if (!$user) return Json::fail('数据不存在!');
        }
      
        $f = array();
        $f[] = Form::hidden('id', $user->getData('id'));
        $f[] = Form::input('nickname', '昵称', $user->getData('nickname'));
        $f[] = Form::input('name', '名称', $user->getData('name'));
        $f[] = Form::input('mobile', '手机号', $user->getData('mobile'));
        $f[] = Form::input('credit_card', '身份证号', $user->getData('credit_card'));
        $f[] = Form::radio('status', '状态', $user->getData('status'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
        $f[] = Form::radio('sex', '性别', $user->getData('sex'))->options([['value' => 1, 'label' => '男'], ['value' => 0, 'label' => '保密'], ['value' => 2, 'label' => '女']]);
        $form = Form::make_post_form('',$f, Url::buildUrl('save', array('id' => $uid)),2);
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }

    /**
     * 表单提交
     *
     * @return json
     */
    public function save(){
        $post_data = Request::post();
        if($post_data['id']){
            $res = UserModel::update($post_data);
        }else{
            $res = UserModel::set($post_data);
        }
        if($res){
             return Json::successful('保存成功');
        }else{
             return Json::fail('保存失败');
        }
    }

}
